package com.topone.controller;

import com.topone.Enum.BussinessStatusEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.BussinessForm;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.Bussiness;
import com.topone.dto.BussinessDTO;
import com.topone.dto.CouponDTO;
import com.topone.dto.TcCouponDTO;
import com.topone.service.BussinessService;
import com.topone.service.TcCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/17,0017.
 */
@RestController
@CrossOrigin
@RequestMapping("/buyer/bussiness")
@Slf4j
public class bussinessController {

    @Autowired
    private BussinessService bussinessService;
    @Autowired
    private TcCouponService tcCouponService;



    @GetMapping("/allBussiness")
    public ResultVO<Map<String,Object>> allBussiness(Map<String,Object> map){
        List<BussinessDTO> bussinessDTOList=new ArrayList<>();
        try {
            bussinessDTOList=bussinessService.allBussiness(
                    BussinessStatusEnum.suceess.getCode()); //查询手游有效商家信息
        }catch (FanZoneException e){
            log.error("【查询所有商家信息】失败",e);
            map.put("error",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }

         map.put("bussinessList",bussinessDTOList);
        log.info("【查询所有商家信息】成功,{}",map);
        return  ResultVOUntil.success(map);
    }


    @GetMapping("/searchBussinessOrCoupon")
    public ResultVO<Map<String,Object>> serachBussiness(@RequestParam("name") String name,
                                        Map<String,Object> map){
        if(StringUtils.isEmpty(name)){
            log.error("【搜索商检信息】参数为空,name={}",name);
             return  ResultVOUntil.error(1,"参数为空");
        }
        try {
            List<BussinessDTO> bussinessList=bussinessService.findBussinessNameList(
                    name);
            map.put("bussinessList",bussinessList);
           List<TcCouponDTO> couponDTOList=tcCouponService.queryCouponNameLikeList(name);
           map.put("couponDTOList",couponDTOList);
        }catch (FanZoneException e){
            map.put("error",e.fillInStackTrace());
            log.error("【根据商家名称查询信息】失败,{}",e.fillInStackTrace());
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【根据商家名称查询信息】成功,{}",map);
        return ResultVOUntil.success(map);
    }

}
